package Demo;

import java.util.concurrent.atomic.AtomicInteger;

public class CASSolution {
    /*
     * 使用三个线程来模拟多线程的场景
     * 使用3条线程，将一个值，从0累加到100
     * */

    //下面是使用CAS的示例
    static AtomicInteger num = new AtomicInteger(0);

    public static void main(String[] args) {
        for (int i = 0; i < 3; i++) {
            Thread thread = new Thread(new Runnable() {
                @Override
                public void run() {
                    while (num.get() < 1000) {
                        System.out.println("Thead Name:" + Thread.currentThread().getName() +" : "+num.incrementAndGet());
                    }
                }
            });
            thread.start();
        }
    }
}
